﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.Config;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;
using log4net;
using ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Themes;
using System.Text.RegularExpressions;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for SummaryPage.xaml
    /// </summary>
    public partial class SummaryPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SummaryPage));
        private readonly CoreConfig Config;
        

        public SummaryPage() {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;


            ThemeComboBox.ItemsSource = WizardTheme.Themes.ToList();
            //Set the current theme
            if(Config.Theme != WizardTheme.Default) {
                ThemeComboBox.SelectedItem = Config.Theme;
            }


            //Construct thumbnails from all of the presentation slides
            MirrorPreviewSlides = GetMirrorSlides(Config);

            if(MirrorPreviewSlides.Count > 0) {
                CurrentSlideIndex = 0;
                SlidePreview.Visibility = System.Windows.Visibility.Visible;
                DisplayMirrorSlide(MirrorPreviewSlides[CurrentSlideIndex]);
            }
            UpdateMirrorButtons();

            //Audience Response
            AudienceResponseQuestions = GetQuestions(Config);
            if(AudienceResponseQuestions.Count > 0) {
                CurrentQuestionIndex = 0;
                DisplayQuestion(AudienceResponseQuestions[CurrentQuestionIndex]);
            }
            UpdateQuestionButtons();

            //Photo Pages
            PhotoPages = GetPhotos(Config);
            if(PhotoPages.Count > 0) {
                CurrentPhotoIndex = 0;
                DisplayPhoto(PhotoPages[CurrentPhotoIndex]);
            }
            UpdatePhotoButtons();
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }


        #region Theme
        private void ThemeChanged(object sender, SelectionChangedEventArgs e) {
            if(e.AddedItems.Count != 1) {
                return;  //Don't handle other changes
            }
            WizardTheme selectedTheme = (WizardTheme)(e.AddedItems[0]);
            //Apply theme changes

        }

        #endregion Theme


        #region Mirror Slides
        private List<PowerPoint.Slide> MirrorPreviewSlides;
        private int CurrentSlideIndex;

        private List<PowerPoint.Slide> GetMirrorSlides(CoreConfig config) {
            //Find all of the slides that are mirror slides

            HashSet<int> mirroredSlideIDs = new HashSet<int>();
            string mirrorNamePattern = @"Mirror_Slide_\d{3}";
            foreach(SlideConfig s in config.Slides) {
                foreach(PageConfig p in s.Pages) {
                    if(p is UserSlideConfig
                        && Regex.IsMatch(p.Name, mirrorNamePattern)) {
                            mirroredSlideIDs.Add(s.SlideId);
                    }
                }
            }

            List<PowerPoint.Slide> presSlides = new List<PowerPoint.Slide>();
            foreach(PowerPoint.Slide s in config.Presentation.Slides) {
                if(mirroredSlideIDs.Contains(s.SlideID)) {
                    presSlides.Add(s);
                }
            }
            return presSlides.OrderBy(s => s.SlideNumber).ToList();
        }

        private void DisplayMirrorSlide(PowerPoint.Slide slide) {
            try {

                SlidePreview.Source = SlideUtil.GetPreview(slide);
                CurrentPreviewSlide = slide;

                PowerPointSlideNumberText.Text = string.Format("PowerPoint Slide {0}", slide.SlideNumber);

            } catch(Exception ex) {
                log.Warn("Cannot export slide", ex);
            }
        }


        public PowerPoint.Slide _currentPreviewSlide;
        public PowerPoint.Slide CurrentPreviewSlide {
            get {
                return _currentPreviewSlide;
            }
            set {
                _currentPreviewSlide = value;
                NotifyPropertyChanged("CurrentPreviewSlide");
            }
        }

        private void PrevSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex > 0) {
                CurrentSlideIndex = CurrentSlideIndex - 1;
                DisplayMirrorSlide(MirrorPreviewSlides[CurrentSlideIndex]);
            }
            UpdateMirrorButtons();
        }

        private void NextSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex < MirrorPreviewSlides.Count - 1) {
                CurrentSlideIndex = CurrentSlideIndex + 1;
                DisplayMirrorSlide(MirrorPreviewSlides[CurrentSlideIndex]);
            }
            UpdateMirrorButtons();
        }

        private void UpdateMirrorButtons() {
            NextSlideButton.IsEnabled = (CurrentSlideIndex < MirrorPreviewSlides.Count - 1);
            PrevSlideButton.IsEnabled = (CurrentSlideIndex > 0);
        }

        private void EditMirrorSlides_Click(object sender, RoutedEventArgs e) {
            NavigationService navService = this.NavigationService;
            if(navService != null) {
                navService.Navigate(new CreateContentPage());
            }
        }
        #endregion Mirror Slides


        #region Audience Response
        private List<PageConfig> AudienceResponseQuestions;
        private int CurrentQuestionIndex;

        private List<PageConfig> GetQuestions(CoreConfig config) {

            List<PageConfig> questionConfigs = new List<PageConfig>();
            foreach(SlideConfig s in config.Slides.OrderBy(p => p.SlideNumber)) {
                foreach(PageConfig p in s.Pages) {
                    if(p is QuestionConfig
                        || p is MultipleChoiceConfig
                        || p is CategoriesConfig) {
                            questionConfigs.Add(p);
                    }
                }
            }
            return questionConfigs;

        }

        private void DisplayQuestion(PageConfig pConfig) {
            AudienceResponsePreviewRoot.Content = PagePreviewFactory.Create(pConfig);
            if(pConfig is QuestionConfig) {
                AudienceResponseTypeText.Text = "Fill-In";
            } else if(pConfig is MultipleChoiceConfig) {
                AudienceResponseTypeText.Text = "Multiple Choice";
            } else if(pConfig is CategoriesConfig) {
                AudienceResponseTypeText.Text = "Categories";
            }
        }

        private void PrevQuestionButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentQuestionIndex > 0) {
                CurrentQuestionIndex = CurrentQuestionIndex - 1;
                DisplayQuestion(AudienceResponseQuestions[CurrentQuestionIndex]);
            }
            UpdateQuestionButtons();
        }

        private void NextQuestionButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentQuestionIndex < AudienceResponseQuestions.Count - 1) {
                CurrentQuestionIndex = CurrentQuestionIndex + 1;
                DisplayQuestion(AudienceResponseQuestions[CurrentQuestionIndex]);
            }
            UpdateQuestionButtons();
        }

        private void UpdateQuestionButtons() {
            NextQuestionButton.IsEnabled = (CurrentQuestionIndex < AudienceResponseQuestions.Count - 1);
            PrevQuestionButton.IsEnabled = (CurrentQuestionIndex > 0);
        }

        #endregion Audience Response


        #region Photos
        private List<PageConfig> PhotoPages;
        private int CurrentPhotoIndex;

        private List<PageConfig> GetPhotos(CoreConfig config) {

            List<PageConfig> photoPages = new List<PageConfig>();
            string namePattern = @"Photo_";
            foreach(SlideConfig s in config.Slides.OrderBy(p => p.SlideNumber)) {
                foreach(PageConfig p in s.Pages) {
                    if(p is UserSlideConfig
                        && Regex.IsMatch(p.Name, namePattern)) {
                            photoPages.Add(p);
                    }
                }
            }
            return photoPages;

        }

        private void DisplayPhoto(PageConfig pConfig) {
            PhotoPreviewRoot.Content = PagePreviewFactory.Create(pConfig);
            PhotoText.Text = pConfig.Name.Remove(0, 6);
        }


        private void PrevPhotoButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentPhotoIndex > 0) {
                CurrentPhotoIndex = CurrentPhotoIndex - 1;
                DisplayPhoto(PhotoPages[CurrentPhotoIndex]);
            }
            UpdatePhotoButtons();
        }

        private void NextPhotoButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentPhotoIndex < PhotoPages.Count - 1) {
                CurrentPhotoIndex = CurrentPhotoIndex + 1;
                DisplayPhoto(PhotoPages[CurrentPhotoIndex]);
            }
            UpdatePhotoButtons();
        }

        private void UpdatePhotoButtons() {
            NextPhotoButton.IsEnabled = (CurrentPhotoIndex < PhotoPages.Count - 1);
            PrevPhotoButton.IsEnabled = (CurrentPhotoIndex > 0);
        }

        #endregion Photos


        #region Video


        private void PrevVideoButton_Click(object sender, RoutedEventArgs e) {

        }

        private void NextVideoButton_Click(object sender, RoutedEventArgs e) {

        }



        #endregion Video


        private void Edit_Click(object sender, RoutedEventArgs e) {

        }

       

       
       
        

        



       
    }
}
